package spring.data.specification.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 描述: TODO:
 * 包名: spring.data.specification.entity.
 * 作者: barton.
 * 日期: 16-7-2.
 * 项目名称: spring-data
 * 版本: 1.0
 * JDK: since 1.8
 */
@Entity
@Table(name = "SYS_RESOURCE")
public class SysResource implements Serializable {

  private static final long serialVersionUID = 1L;

  public static enum SysResourceType {
    MENU("菜单"), BUTTON("按钮");

    private final String info;

    private SysResourceType(String info) {
      this.info = info;
    }

    public String getInfo() {
      return info;
    }
  }

  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  @Column(name = "sysResource_ID")
  private Long id;

  private String name;
  @Enumerated(EnumType.ORDINAL)
  private SysResourceType type;
  private String permission;//权限字符串
  private String url;
  private int priority;
  @Temporal(TemporalType.TIMESTAMP)
  private Date createTime;
  @Column(name = "menu_icon")
  private String icon; //图标
  //	@ManyToMany
//	@JoinTable(name = "sys_SysResources_SysRoles",joinColumns=@JoinColumn(name="SysResource_id"),inverseJoinColumns=@JoinColumn(name="SysRole_id"))
  @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE},
   mappedBy = "sysResource")
  private Set<SysRole> sysRoles = new HashSet<>();

//	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST,	CascadeType.MERGE}, mappedBy = "menus")
//	private Set<SysRoleEntity> SysRoles;

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade = {CascadeType.PERSIST})
  private Collection<SysResource> children = new HashSet<>();

  @ManyToOne
  @JoinColumn(name = "parent_id")
  private SysResource parent;

  public SysResource() {
  }

  public SysResource(String name, SysResourceType type, String url, int priority, Date createTime) {
    super();
    this.name = name;
    this.url = url;
    this.priority = priority;
    this.type = type;
    this.createTime = createTime;
  }

  public String getIcon() {
    return icon;
  }

  public void setIcon(String icon) {
    this.icon = icon;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public int getPriority() {
    return priority;
  }

  public void setPriority(int priority) {
    this.priority = priority;
  }

  public SysResource getParent() {
    return parent;
  }

  public void setParent(SysResource parent) {
    this.parent = parent;
  }

  public Set<SysRole> getSysRoles() {
    return sysRoles;
  }

  public void addSysRole(SysRole sysRoles) {
    this.sysRoles.add(sysRoles);
  }

  public void removeSysRole(SysRole sysRoles) {
    this.sysRoles.remove(sysRoles);
  }

  public void setSysRoles(Set<SysRole> sysRoles) {
    this.sysRoles = sysRoles;
  }

  public SysResourceType getType() {
    return type;
  }

  public void setType(SysResourceType type) {
    this.type = type;
  }

  public String getPermission() {
    return permission;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public void setPermission(String permission) {
    this.permission = permission;
  }

  public Collection<SysResource> getChildren() {
    return children;
  }

  public void setChildren(Collection<SysResource> children) {
    this.children = children;
  }

  @Override
  public String toString() {
    return "SysResource [id=" + id + ", name=" + name + ", type=" + type
     + ", permission=" + permission + ", url=" + url + ", priority="
     + priority + ", createTime=" + createTime + ", icon=" + icon
     + ", SysRoles=" + sysRoles
     + "]";
  }

}
